"""
数据访问层 (DAO) 包
提供所有金融数据的专用访问接口
"""

# 基础DAO类
from .base import (
    BaseDAO,
    FinancialDataDAO,
    DAOFactory
)

# 股票相关DAO
from .stock import (
    StockBasicInfoDAO,
    StockDailyDataDAO,
    StockFinancialDataDAO,
    StockHolderDataDAO,
    StockDividendDataDAO,
    StockMarginDataDAO,
    StockMinuteDataDAO,
    StockTickDataDAO
)

# 债券相关DAO
from .bond import (
    BondBasicInfoDAO,
    BondDailyDataDAO,
    BondConvertibleDataDAO,
    BondYieldDataDAO,
    BondIssueDataDAO
)

# 基金相关DAO
from .fund import (
    FundBasicInfoDAO,
    FundNetValueDAO,
    FundPortfolioDAO,
    FundManagerDAO,
    FundDividendDAO,
    FundPerformanceDAO
)

# 期货相关DAO
from .futures import (
    FuturesBasicInfoDAO,
    FuturesDailyDataDAO,
    FuturesMinuteDataDAO,
    FuturesHoldingRankDAO,
    FuturesWarehouseReceiptDAO,
    FuturesSpotPriceDAO
)

# 期权相关DAO
from .option import (
    OptionBasicInfoDAO,
    OptionDailyDataDAO,
    OptionMinuteDataDAO,
    OptionChainDAO,
    OptionExerciseDataDAO
)

# 宏观经济相关DAO
from .macro import (
    MacroEconomicIndicatorDAO,
    MonetaryPolicyDAO,
    FiscalDataDAO,
    TradeDataDAO,
    EnergyDataDAO,
    RealEstateDataDAO
)

# 指数相关DAO
from .index import (
    IndexBasicInfoDAO,
    IndexDailyDataDAO,
    IndexConstituentDAO,
    IndexMinuteDataDAO,
    IndexValuationDAO,
    IndexPerformanceDAO
)

# 新闻相关DAO
from .news import (
    NewsArticleDAO,
    NewsEventDAO,
    NewsResearchDAO,
    NewsAnnouncementDAO
)

# 所有DAO类的列表，用于批量操作
ALL_DAO_CLASSES = [
    # 基础DAO
    BaseDAO,
    FinancialDataDAO,
    
    # 股票DAO
    StockBasicInfoDAO,
    StockDailyDataDAO,
    StockFinancialDataDAO,
    StockHolderDataDAO,
    StockDividendDataDAO,
    StockMarginDataDAO,
    StockMinuteDataDAO,
    StockTickDataDAO,
    
    # 债券DAO
    BondBasicInfoDAO,
    BondDailyDataDAO,
    BondConvertibleDataDAO,
    BondYieldDataDAO,
    BondIssueDataDAO,
    
    # 基金DAO
    FundBasicInfoDAO,
    FundNetValueDAO,
    FundPortfolioDAO,
    FundManagerDAO,
    FundDividendDAO,
    FundPerformanceDAO,
    
    # 期货DAO
    FuturesBasicInfoDAO,
    FuturesDailyDataDAO,
    FuturesMinuteDataDAO,
    FuturesHoldingRankDAO,
    FuturesWarehouseReceiptDAO,
    FuturesSpotPriceDAO,
    
    # 期权DAO
    OptionBasicInfoDAO,
    OptionDailyDataDAO,
    OptionMinuteDataDAO,
    OptionChainDAO,
    OptionExerciseDataDAO,
    
    # 宏观经济DAO
    MacroEconomicIndicatorDAO,
    MonetaryPolicyDAO,
    FiscalDataDAO,
    TradeDataDAO,
    EnergyDataDAO,
    RealEstateDataDAO,
    
    # 指数DAO
    IndexBasicInfoDAO,
    IndexDailyDataDAO,
    IndexConstituentDAO,
    IndexMinuteDataDAO,
    IndexValuationDAO,
    IndexPerformanceDAO,
    
    # 新闻DAO
    NewsArticleDAO,
    NewsEventDAO,
    NewsResearchDAO,
    NewsAnnouncementDAO
]

# 按类型分组的DAO类
DAO_GROUPS = {
    'stock': [
        StockBasicInfoDAO,
        StockDailyDataDAO,
        StockFinancialDataDAO,
        StockHolderDataDAO,
        StockDividendDataDAO,
        StockMarginDataDAO,
        StockMinuteDataDAO,
        StockTickDataDAO
    ],
    'bond': [
        BondBasicInfoDAO,
        BondDailyDataDAO,
        BondConvertibleDataDAO,
        BondYieldDataDAO,
        BondIssueDataDAO
    ],
    'fund': [
        FundBasicInfoDAO,
        FundNetValueDAO,
        FundPortfolioDAO,
        FundManagerDAO,
        FundDividendDAO,
        FundPerformanceDAO
    ],
    'futures': [
        FuturesBasicInfoDAO,
        FuturesDailyDataDAO,
        FuturesMinuteDataDAO,
        FuturesHoldingRankDAO,
        FuturesWarehouseReceiptDAO,
        FuturesSpotPriceDAO
    ],
    'option': [
        OptionBasicInfoDAO,
        OptionDailyDataDAO,
        OptionMinuteDataDAO,
        OptionChainDAO,
        OptionExerciseDataDAO
    ],
    'macro': [
        MacroEconomicIndicatorDAO,
        MonetaryPolicyDAO,
        FiscalDataDAO,
        TradeDataDAO,
        EnergyDataDAO,
        RealEstateDataDAO
    ],
    'index': [
        IndexBasicInfoDAO,
        IndexDailyDataDAO,
        IndexConstituentDAO,
        IndexMinuteDataDAO,
        IndexValuationDAO,
        IndexPerformanceDAO
    ],
    'news': [
        NewsArticleDAO,
        NewsEventDAO,
        NewsResearchDAO,
        NewsAnnouncementDAO
    ]
}

# 导出所有类和常量
__all__ = [
    # 基础DAO类
    'BaseDAO',
    'FinancialDataDAO',
    'DAOFactory',
    
    # 股票相关DAO
    'StockBasicInfoDAO',
    'StockDailyDataDAO',
    'StockFinancialDataDAO',
    'StockHolderDataDAO',
    'StockDividendDataDAO',
    'StockMarginDataDAO',
    'StockMinuteDataDAO',
    'StockTickDataDAO',
    
    # 债券相关DAO
    'BondBasicInfoDAO',
    'BondDailyDataDAO',
    'BondConvertibleDataDAO',
    'BondYieldDataDAO',
    'BondIssueDataDAO',
    
    # 基金相关DAO
    'FundBasicInfoDAO',
    'FundNetValueDAO',
    'FundPortfolioDAO',
    'FundManagerDAO',
    'FundDividendDAO',
    'FundPerformanceDAO',
    
    # 期货相关DAO
    'FuturesBasicInfoDAO',
    'FuturesDailyDataDAO',
    'FuturesMinuteDataDAO',
    'FuturesHoldingRankDAO',
    'FuturesWarehouseReceiptDAO',
    'FuturesSpotPriceDAO',
    
    # 期权相关DAO
    'OptionBasicInfoDAO',
    'OptionDailyDataDAO',
    'OptionMinuteDataDAO',
    'OptionChainDAO',
    'OptionExerciseDataDAO',
    
    # 宏观经济相关DAO
    'MacroEconomicIndicatorDAO',
    'MonetaryPolicyDAO',
    'FiscalDataDAO',
    'TradeDataDAO',
    'EnergyDataDAO',
    'RealEstateDataDAO',
    
    # 指数相关DAO
    'IndexBasicInfoDAO',
    'IndexDailyDataDAO',
    'IndexConstituentDAO',
    'IndexMinuteDataDAO',
    'IndexValuationDAO',
    'IndexPerformanceDAO',
    
    # 新闻相关DAO
    'NewsArticleDAO',
    'NewsEventDAO',
    'NewsResearchDAO',
    'NewsAnnouncementDAO',
    
    # 常量
    'ALL_DAO_CLASSES',
    'DAO_GROUPS'
]


def get_dao_by_model_name(model_name: str):
    """
    根据模型名称获取对应的DAO类
    
    Args:
        model_name: 模型类名称
        
    Returns:
        对应的DAO类，如果未找到则返回None
    """
    dao_mapping = {
        # 股票模型映射
        'StockBasicInfo': StockBasicInfoDAO,
        'StockDailyData': StockDailyDataDAO,
        'StockFinancialData': StockFinancialDataDAO,
        'StockHolderData': StockHolderDataDAO,
        'StockDividendData': StockDividendDataDAO,
        'StockMarginData': StockMarginDataDAO,
        'StockMinuteData': StockMinuteDataDAO,
        'StockTickData': StockTickDataDAO,
        
        # 债券模型映射
        'BondBasicInfo': BondBasicInfoDAO,
        'BondDailyData': BondDailyDataDAO,
        'BondConvertibleData': BondConvertibleDataDAO,
        'BondYieldData': BondYieldDataDAO,
        'BondIssueData': BondIssueDataDAO,
        
        # 基金模型映射
        'FundBasicInfo': FundBasicInfoDAO,
        'FundNetValue': FundNetValueDAO,
        'FundPortfolio': FundPortfolioDAO,
        'FundManager': FundManagerDAO,
        'FundDividend': FundDividendDAO,
        'FundPerformance': FundPerformanceDAO,
        
        # 期货模型映射
        'FuturesBasicInfo': FuturesBasicInfoDAO,
        'FuturesDailyData': FuturesDailyDataDAO,
        'FuturesMinuteData': FuturesMinuteDataDAO,
        'FuturesHoldingRank': FuturesHoldingRankDAO,
        'FuturesWarehouseReceipt': FuturesWarehouseReceiptDAO,
        'FuturesSpotPrice': FuturesSpotPriceDAO,
        
        # 期权模型映射
        'OptionBasicInfo': OptionBasicInfoDAO,
        'OptionDailyData': OptionDailyDataDAO,
        'OptionMinuteData': OptionMinuteDataDAO,
        'OptionChain': OptionChainDAO,
        'OptionExerciseData': OptionExerciseDataDAO,
        
        # 宏观经济模型映射
        'MacroEconomicIndicator': MacroEconomicIndicatorDAO,
        'MonetaryPolicy': MonetaryPolicyDAO,
        'FiscalData': FiscalDataDAO,
        'TradeData': TradeDataDAO,
        'EnergyData': EnergyDataDAO,
        'RealEstateData': RealEstateDataDAO,
        
        # 指数模型映射
        'IndexBasicInfo': IndexBasicInfoDAO,
        'IndexDailyData': IndexDailyDataDAO,
        'IndexConstituent': IndexConstituentDAO,
        'IndexMinuteData': IndexMinuteDataDAO,
        'IndexValuation': IndexValuationDAO,
        'IndexPerformance': IndexPerformanceDAO,
        
        # 新闻模型映射
        'NewsArticle': NewsArticleDAO,
        'NewsEvent': NewsEventDAO,
        'NewsResearch': NewsResearchDAO,
        'NewsAnnouncement': NewsAnnouncementDAO
    }
    
    return dao_mapping.get(model_name)


def get_dao_group(group_name: str):
    """
    根据组名获取DAO类列表
    
    Args:
        group_name: 组名 ('stock', 'bond', 'fund', 'futures', 'option', 'macro', 'index', 'news')
        
    Returns:
        DAO类列表，如果未找到则返回空列表
    """
    return DAO_GROUPS.get(group_name, [])


def create_all_dao_instances(session):
    """
    创建所有DAO实例
    
    Args:
        session: 数据库会话
        
    Returns:
        包含所有DAO实例的字典
    """
    dao_instances = {}
    
    for dao_class in ALL_DAO_CLASSES:
        if hasattr(dao_class, 'model') and dao_class.model:
            # 使用模型名称作为键
            model_name = dao_class.model.__name__
            dao_instances[model_name] = dao_class()
    
    return dao_instances